#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QClipboard>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    changelabelState(0);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changelabelState(uint8_t state)
{
    QPalette pe;
    switch(state)
    {
        case 0 :
        {
            ui->label_state->setText("设备未连接");
            pe.setColor(QPalette::WindowText,Qt::red);
            ui->label_state->setPalette(pe);
            break;
        }
        case 1 :
        {
            ui->label_state->setText("设备已连接");
            pe.setColor(QPalette::WindowText,Qt::green);
            ui->label_state->setPalette(pe);
            break;
        }
            default:
        {

        }
    }
}

void MainWindow::deviceClose()
{
    ui->textBrowser->append("[Sys]设备断开连接!");
    on_button_reload_clicked();
    //串口关闭的状态
    ui->comboBox->setEnabled(true);
    ui->button_reload->setEnabled(true);
    ui->button_connect->setEnabled(true);
    serial->device.close();
    changelabelState(0);
}

void MainWindow::showUID(QString uid)
{
    ui->lineEdit->setText(uid);
}

/*刷新串口列表*/
void MainWindow::on_button_reload_clicked()
{
    //获取串口列表
    QList<QSerialPortInfo> list = Serial::getSerial()->getAllSerial();
    //调试输出
//    for (int i = 0;i < list.size();i++)
//    {
//        qDebug() << list.at(i).portName()<<list.at(i).description();
//    }

    //清空comboBox
    ui->comboBox->clear();

    //判断个数
    if(list.size() == 0)
    {
        ui->textBrowser->append("[System]未搜索到串口设备!");
        return;
    }


    //添加到comboBox
    for (int i = 0;i < list.size();i++) {
        ui->comboBox->addItem(list.at(i).portName());
    }
    ui->textBrowser->append(QString("[System]搜索到%1个串口设备!").arg(list.size()));

}

void MainWindow::on_button_connect_clicked()
{
    bool flag = serial->serialOpen(ui->comboBox->currentText());
    if(false == flag)
    {
//        ui->textBrowser->append("[Sys]串口打开失败!");
        on_button_reload_clicked();
        return;
    }

    //可以判断一下是不是pn532

    ui->textBrowser->append("[Sys]串口打开成功!");
    //串口打开的状态
    ui->comboBox->setEnabled(false);
    ui->button_reload->setEnabled(false);
    ui->button_connect->setEnabled(false);
    changelabelState(1);


    //绑定串口错误处理函数
    connect(&serial->device,&QSerialPort::errorOccurred,this,&MainWindow::serialErrorHandle);
    //绑定接收处理函数
    connect(&serial->device,&QSerialPort::readyRead,this,&MainWindow::serialReadyRead_Handle);
}

void MainWindow::serialErrorHandle(QSerialPort::SerialPortError error)
{
    switch(error)
    {
        case 2:{
            ui->textBrowser->append("[Sys]设备断开连接!");
            on_button_reload_clicked();
            //串口关闭的状态
            ui->comboBox->setEnabled(true);
            ui->button_reload->setEnabled(true);
            ui->button_connect->setEnabled(true);
            serial->device.close();
            changelabelState(0);
            break;
        }
        case 3:{
            ui->textBrowser->append("[Sys]设备已连接无法重复操作!");
            break;
        }
        default:
        {

        }
    }
}

void MainWindow::on_button_disconnect_clicked()
{
    deviceClose();
}

void MainWindow::on_button_clear_clicked()
{
    ui->textBrowser->clear();
}

void MainWindow::on_button_weakup_clicked()
{
    serial->sendCommand_weakup();
}

void MainWindow::serialReadyRead_Handle()
{
    uint8_t returnsuccess[15] = {0x00,0x00,0xFF,0x00,0xFF,0x00,0x00,0x00,0xFF,0x02,0xFE,0xD5,0x15,0x16,0x00};
    uint8_t box[30];
    QByteArray data = serial->device.readAll();
    QString str;
    for(int i = 0;i < data.size();i++)
    {
        box[i] = data.at(i);
    }
    ui->textBrowser->append(str);
    if(memcmp(box,returnsuccess,15) == 0)
    {
        ui->textBrowser->append("[System]唤醒成功!");
    }
    if(data.size() == 6)
    {
        ui->textBrowser->append("[System]读卡:请把标签放入识别区域...");
    }
    qDebug() << "收到" << data.size() << "个字节的数据";
    if(data.size() == 22)
    {
        ui->textBrowser->append("[System]读取到UID卡号:");
        QString uid;
        for (int i = 0;i < 7;i++)
        {
            if((uint8_t)data.at(13+i)<16)
            {
                uid+="0";
            }
            uid+=QString::number((uint8_t)data.at(13+i),16);
            uid+=":";
        }
        //去掉最后一个":"
        uid = uid.left(uid.size()-1);
        ui->textBrowser->append(uid);
        showUID(uid);
    }


}

void MainWindow::on_button_read_clicked()
{
    serial->sendCommand_find();
}

void MainWindow::on_button_copy_clicked()
{
    QClipboard *clipboard = QApplication::clipboard();   //获取系统剪贴板指针
    clipboard->setText(ui->lineEdit->text());
    ui->textBrowser->append("[System]复制成功!");
}
